Node中使用ES6语法

目前nodejs的高级版本对es6的支持越来越大,大部分的es2015的特性在node高版本中不需要babel转码就可以直接使用。涉及到对es2016以及es2017的一些特性的支持目前还不是很广泛,只能借助于babel的转码编译。如 async/await 特性,... 对象扩展运算符等。下文介绍了在部分特性无法得到支持的情况下,如何在node环境中使用es6语法。

更新: Node7 通过 --harmony_async_await 参数已经支持了async/await 特性。

es-checker 用于检查当前node对于es6的支持情况

npm install -g es-checker

安装完毕后,执行

es-checker

示例: (不同版本node运行该命令后的结果不同)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
ECMAScript 6 Feature Detection (v1.4.1)

Variables
let and const
√ TDZ error for too-early access of let or const declarations
√ Redefinition of const declarations not allowed
√ destructuring assignments/declarations for arrays and objects
√ ... operator

Data Types
√ For...of loop
√ Map, Set, WeakMap, WeakSet
√ Symbol
√ Symbols cannot be implicitly coerced

Number
√ Octal (e.g. 0o1 ) and binary (e.g. 0b10 ) literal forms
√ Old octal literal invalid now (e.g. 01 )
√ Static functions added to Math (e.g. Math.hypot(), Math.acosh(), Math.imul() )
√ Static functions added to Number (Number.isNaN(), Number.isInteger() )

String
√ Methods added to String.prototype (String.prototype.includes(), String.prototype.repeat() )
√ Unicode code-point escape form in string literals (e.g. \u{20BB7} )
√ Unicode code-point escape form in identifier names (e.g. var \u{20BB7} = 42; )
√ Unicode code-point escape form in regular expressions (e.g. var regexp = /\u{20BB7}/u; )
√ y flag for sticky regular expressions (e.g. /b/y )
√ Template String Literals

Function
√ arrow function
√ default function parameter values
√ destructuring for function parameters
× Inferences for function name property for anonymous functions
× Tail-call optimization for function calls and recursion

Array
× Methods added to Array.prototype ([].fill(), [].find(), [].findIndex(), [].entries(), [].keys(), [].values() )
√ Static functions added to Array (Array.from(), Array.of() )
√ TypedArrays like Uint8Array, ArrayBuffer, Int8Array(), Int32Array(), Float64Array()
√ Some Array methods (e.g. Int8Array.prototype.slice(), Int8Array.prototype.join(), Int8Array.prototype.forEach() ) added to the TypedArray prototypes
√ Some Array statics (e.g. Uint32Array.from(), Uint32Array.of() ) added to the TypedArray constructors

Object
√ __proto__ in object literal definition sets [[Prototype]] link
√ Static functions added to Object (Object.getOwnPropertySymbols(), Object.assign() )
√ Object Literal Computed Property
√ Object Literal Property Shorthands
√ Proxies
√ Reflect

Generator and Promise
√ Generator function
√ Promises

Class
√ Class
√ super allowed in object methods
√ class ABC extends Array { .. }

Module
× Module export command
× Module import command


=========================================
Passes 37 feature Detections
Your runtime supports 88% of ECMAScript 6
=========================================

node中添加babel实现对es6的转码

  1. 当前文件夹下如果没有package.json的话,新建并初始化
  2. 安装babel-cli, npm install babel-cli --save-dev
  3. 安装babel的presets, npm install babel-preset-es2015 --save-dev, 此时可以使用es2015的特性
  4. 如若使用async/await等更高级别的特性需要安装额外的插件,如 npm install babel-preset-stage-0
  5. 安装babel运行时转码工具,transform-runtime插件,npm install babel-plugin-transform-runtime --save-dev 以及 npm install babel-runtime
  6. 新建 .babelrc文件, 添加如下配置:
1
2
3
4
5
6
7
8
9
10
11
{
"presets": ["es2015", "stage-0"],
"plugins": [
["transform-runtime", {
"helpers": false,
"polyfill": false,
"regenerator": true,
"moduleName": "babel-runtime"
}]
]
}
  1. package.json 文件中添加 scripts 字段,并配置为 ./node_modules/.bin/babel-node index.js
  2. 执行 npm run start 命令就可以愉快的在node中玩耍es6了
作者

monster1935

发布于

2017-06-27

更新于

2024-09-25

许可协议